package com.atguigu.tingshu.album.task;

import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.atguigu.tingshu.common.constant.SystemConstant.*;

/**
 * @author: atguigu
 * @create: 2025-09-22 15:55
 */


@Slf4j
@Component
public class ReviewTask {

    @Autowired
    private TrackInfoMapper trackInfoMapper;

    @Autowired
    private AuditService auditService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void updateTrackInfoStatus() {
        log.info("开始执行定时任务：更新专辑状态");
        //获取正在审核中的专辑
        List<TrackInfo> trackInfos = trackInfoMapper.selectList(
                new LambdaQueryWrapper<TrackInfo>().eq(TrackInfo::getStatus, TRACK_STATUS_ING)
                        .select(TrackInfo::getId, TrackInfo::getReviewTaskId)
                        .last("limit 100"));
        //循环便利出所需要的数据
        for (TrackInfo trackInfo : trackInfos) {
            String reviewTaskId = trackInfo.getReviewTaskId();
            String reviewResult = auditService.getReviewResult(reviewTaskId);
            if (reviewResult != null) {
                if ("pass".equals(reviewResult)) {
                    trackInfo.setStatus(TRACK_STATUS_PASS);
                } else if ("block".equals(reviewResult)) {
                    trackInfo.setStatus(TRACK_STATUS_NO_PASS);
                } else if ("review".equals(reviewResult)) {
                    trackInfo.setStatus(TRACK_STATUS_REVIEWING);
                }
            }
            trackInfoMapper.updateById(trackInfo);
        }
        log.info("定时任务执行完毕：更新专辑状态");
    }

}
